package com.hz.basic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hz.basic.dao.CbExpandResourceDao;
import com.hz.basic.entity.CbExpandResourceEntity;
import com.hz.basic.service.CbExpandResourceService;
import com.hz.common.utils.PageUtils;
import com.hz.common.utils.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service("cbExpandResourceService")
public class CbExpandResourceServiceImpl extends ServiceImpl<CbExpandResourceDao, CbExpandResourceEntity> implements CbExpandResourceService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CbExpandResourceEntity> page = this.page(
                new Query<CbExpandResourceEntity>().getPage(params),
                new QueryWrapper<CbExpandResourceEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils getexpandresource(Long cid,String condition,int curpage, int numpage) {
        int total = this.baseMapper.getexpandresourcetotal(cid, condition);
        //不合理的curpage，越界
        if(curpage <= 0 || Math.ceil((double)total/numpage) < curpage)
            return new PageUtils(new ArrayList<CbExpandResourceEntity>(),total,numpage,curpage);
        List<CbExpandResourceEntity> getexpandresources = this.baseMapper.getexpandresource(cid, condition, (curpage-1) * numpage, numpage);
        PageUtils page = new PageUtils(getexpandresources,total,numpage,curpage);
        return page;
    }

    @Override
    public PageUtils getallexpandresource(Long cid,String condition,int curpage, int numpage) {
        int total = this.baseMapper.getallexpandresourcetotal(cid, condition);
        //不合理的curpage，越界
        if(curpage <= 0 || Math.ceil((double)total/numpage) < curpage)
            return new PageUtils(new ArrayList<CbExpandResourceEntity>(),total,numpage,curpage);
        List<CbExpandResourceEntity> getexpandresources = this.baseMapper.getallexpandresource(cid,condition,(curpage-1)*numpage,numpage);
        PageUtils page = new PageUtils(getexpandresources,total,numpage,curpage);
        return page;
    }

    @Override
    public PageUtils getallstiffexpandresource(Long cid,String condition,int curpage, int numpage) {
        int total = this.baseMapper.getallstiffexpandresourcetotal(cid, condition);
        //不合理的curpage，越界
        if(curpage <= 0 || Math.ceil((double)total/numpage) < curpage)
            return new PageUtils(new ArrayList<CbExpandResourceEntity>(),total,numpage,curpage);
        List<CbExpandResourceEntity> getallstiffexpandresources = this.baseMapper.getallstiffexpandresource(cid,condition,(curpage-1)*numpage,numpage);
        PageUtils page = new PageUtils(getallstiffexpandresources,total,numpage,curpage);
        return page;
    }

}